<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Http\Controllers\Controller;
use App\Http\Requests\GroupRequest;
use App\Http\Requests\GroupJoinRequest;
use App\Http\Requests\RemoveUsersRequest;
use App\Models\Group;
use App\Http\Resources\Group as GroupResource;
use App\Http\Resources\GroupFull as GroupFullResource;
use App\Http\Resources\GroupUser as GroupUserResource;
use App\Http\Resources\GroupUserCollection;
use App\Repositories\GroupRepository;

/**
 * @resource 俱乐部
 * Class GroupController
 * @package App\Http\Controllers\Api
 */
class GroupController extends Controller
{
    /**
     * 我的俱乐部
     *
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
     */
    public function list(Request $request, GroupRepository $groupRepository)
    {
        $groups = $groupRepository->findGroupPaginateByUser($request->user());
        return GroupResource::collection($groups);
    }

    /**
     * 俱乐部详情
     *
     * @param $id
     * @return GroupFullResource
     */
    public function show(Group $group)
    {
        return new GroupFullResource($group);
    }

    /**
     * 俱乐部成员
     *
     * @param Group $group
     * @return GroupUserCollection
     */
    public function users(Group $group)
    {
        $data = $group
            ->users()
            ->orderBy('group_user.created_at', 'desc')
            ->paginate();

        return new GroupUserCollection($data);
    }


    /**
     * 俱乐部成员铜币排行榜
     *
     * @param Group $group
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
     */
    public function usersLeaderboard(Group $group)
    {
        $data = $group
            ->users()
            ->orderBy('group_user.copper', 'desc')
            ->paginate();

        return GroupUserResource::collection($data);
    }

    /**
     * 加入俱乐部
     *
     * @param Group $group
     * @param GroupJoinRequest $request
     * @param GroupRepository $groupRepository
     */
    public function join(Group $group,
                         GroupJoinRequest $request,
                         GroupRepository $groupRepository)
    {
        $groupRepository->addUser($group, $request->user());
    }

    /**
     * 删除俱乐部成员
     *
     * @param Group $group
     * @param RemoveUsersRequest $request
     * @param GroupRepository $groupRepository
     */
    public function removeUser(Group $group,
                               RemoveUsersRequest $request,
                               GroupRepository $groupRepository)
    {
        $this->authorize('removeUser', $group);
        $groupRepository->removeUser($group, $request->input('users'));
    }

    /**
     * 创建俱乐部
     *
     * @param GroupRequest $request
     * @param Group $group
     * @return GroupFullResource
     */
    public function create(GroupRequest $request, Group $group)
    {
        $group->fill($request->all());
        if ($request->file('logo')) {
            $group->logo = $this->upload($request);
        }
        $group->save();
        return new GroupFullResource($group);
    }

    /**
     * 修改俱乐部
     *
     * @param Group $group
     * @param GroupRequest $request
     * @param GroupRepository $groupRepository
     */
    public function modify(Group $group,
                           GroupRequest $request,
                           GroupRepository $groupRepository)
    {
        $oldCopper = $group->copper;
        $group->fill($request->all());
        if ($request->file('logo')) {
            Storage::disk('public')->delete($group->logo);
            $group->logo = $this->upload($request);
        }
        $group->save();

        if ($oldCopper != $group->copper) {
            $groupRepository->addCopper($group, (int)($group->copper-$oldCopper));
        }
        return new GroupResource($group);
    }

    /**
     * 删除俱乐部
     *
     * @param Group $group
     */
    public function dissolution(Group $group)
    {
        $group->delete();
    }

    /**
     * @param GroupRequest $request
     * @return string
     */
    private function upload(GroupRequest $request)
    {
        $file = $request->file('logo');
        return Storage::putFileAs(
            'logos', $file, str_random(24).'.'.$file->getClientOriginalExtension()
        );
    }
}
